<?php
 function load($detailid, $filename, $date, $hospitalname){		
		$targetfile=FILES.DS.$date.DS.$hospitalname.DS.$filename;				
		if(file_exists($targetfile)){
			$ames=array();
			$drugs=array();
			$others=array();
			$category=0;
			$isnewline=1;
			$token="";
			$handle = fopen($targetfile, "r");
			while(!feof($handle)){
				$ch=fread($handle,1);
				if($ch=="|"){
					if($category==1){$ames[]=$token;}
					else if($category==2){$drugs[]=$token;}
					else if($category==3){$others[]=$token;}									
					$token="";
				}
				else if($ch=="\r"){				
					if($category==1){$ames[]=$token;}
					else if($category==2){$drugs[]=$token;}
					else if($category==3){$others[]=$token;}
					$token="";
					$isnewline=1;
					if($category==1){$this->ames($ames);}                                                                                                                                            					
					else if($category==2){$this->drugs($drugs);}
					else if($category==3){$this->others($others);}
					
					unset($ames);			
					unset($drugs);
					unset($others);
				}
				else{					
					$token.=$ch;
					if($isnewline==1){
						if($token=='1'){$category=1;}
						else if($token=='2'){$category=2;}
						else if($token=='3'){$category=3;}
						$isnewline=0;
					}
				}
			}					
			if(count($token)>0){
				if($category==1){$ames[]=$token;}
				else if($category==2){$drugs[]=$token;}
				else if($category==3){$others[]=$token;}
				//var_dump($ames);
				if($category==1){$this->ames($ames);}                                                                                                                                            					
				else if($category==2){$this->drugs($drugs);}
				else if($category==3){$this->others($others);}
						
				unset($ames);		
				unset($drugs);
				unset($others);
			}
			$this->requestAction('/details/setStatus', array('pass'=>array($detailid, 1)));
			$this->redirect('/patients/index');
		}	
		else{$this->redirect('/loads/filenotfound');}	
	} 
?>